package com.speedchina.doormanager.handler;

import com.speedchina.doormanager.exception.GlobalException;
import com.speedchina.doormanager.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@ControllerAdvice
public class GlobalExceptionHandler {

    private Logger log= LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = GlobalException.class)
    @ResponseBody
    public ResultVO handlerSellerException(GlobalException e) {
        return ResultVO.error(e.getCode(), e.getMessage());
    }


    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public ResultVO handlerBindException(BindException e) {

        log.info("[参数校验]---绑定异常");

        List<ObjectError> errors = e.getAllErrors();
        ObjectError error = errors.get(0);
        String msg = error.getDefaultMessage();
        return ResultVO.error(100, String.format("参数校验异常：%s", msg));
    }






}
